// pages/plugins/telephoneBook/list/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 是否输入
    searchInputVisible: false,
    // 搜索的内容
    search: '',
    //动态下拉框属性
    animationData: [],
    iconAnimation: [],
    isAnimation: [],
    // 列表
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadTelephoneBookList();
    options.pageName && (wx.setNavigationBarTitle({
      title: options.pageName,
    }))
  },

  // 加载通讯录列表
  loadTelephoneBookList(){
    var that = this;
    var params = {
      point_url: app.server.agentApi.getTelephoneBookList,
      search: this.data.search
    }
    app.server.getConnect(params).then(function (res) {
      that.setData({
        list: res.data
      })
      that.animation = [];
      that.iconAnimation = [];
      for (var i = 0; i < res.data.length; i++) {
        that.initAnimation();
      }
    })
  },

  initAnimation() {
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    })
    var iconAnimation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    })

    this.animation.push(animation);
    this.iconAnimation.push(iconAnimation);

    this.data.animationData.push({});
    this.data.iconAnimation.push({});
    this.data.isAnimation.push(false);
    console.log(this.animation)
    console.log(this.data.isAnimation)
  },

  // 点击输入
  bindInputTap() {
    this.setData({
      searchInputVisible: true
    })
  },

  // 搜索框输入
  bindSearchInput(e) {
    this.setData({
      search: e.detail.value
    })
    var that = this;
    this.loadTelephoneBookList();
  },

  // 取消输入
  bindCancelInput() {
    this.setData({
      searchInputVisible: false
    })
  },

  // 跳转详情
  bindToDetails(e){
    console.log(e)
    wx.navigateTo({
      url: '../details/index?id='+e.currentTarget.dataset.id
    });
  },

  // 动态下拉框下拉收缩方法
  doAnimation: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var query = wx.createSelectorQuery();
    query.select('#dropdown-content' + id).boundingClientRect()
    query.exec((res) => {
      var height = res[0].height;
      that.animation[id].height(that.data.isAnimation[id] ? 0 : height).step();
      that.iconAnimation[id].rotate(that.data.isAnimation[id] ? 0 : 90).step()

      that.data.animationData[id] = that.animation[id].export();
      that.data.isAnimation[id] = !that.data.isAnimation[id];
      that.data.iconAnimation[id] = that.iconAnimation[id].export();
      that.setData({
        animationData: that.data.animationData,
        isAnimation: that.data.isAnimation,
        iconAnimation: that.data.iconAnimation
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadTelephoneBookList();
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
